package com.ys.util.amazon;

import com.amazon.SellingPartnerAPIAA.*;
import org.springframework.util.StringUtils;

/**
 * @Author: yipeng.liu
 * @Date: 2022/11/21/16:31
 * @Description
 */
public class ApiClientBuilder {

    private AWSAuthenticationCredentials awsAuthenticationCredentials;
    private LWAAuthorizationCredentials lwaAuthorizationCredentials;
    private String endpoint;
    private LWAAccessTokenCache lwaAccessTokenCache;
    private Boolean disableAccessTokenCache = false;
    private AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider;

    public ApiClientBuilder awsAuthenticationCredentials(AWSAuthenticationCredentials awsAuthenticationCredentials) {
        this.awsAuthenticationCredentials = awsAuthenticationCredentials;
        return this;
    }

    public ApiClientBuilder lwaAuthorizationCredentials(LWAAuthorizationCredentials lwaAuthorizationCredentials) {
        this.lwaAuthorizationCredentials = lwaAuthorizationCredentials;
        return this;
    }

    public ApiClientBuilder endpoint(String endpoint) {
        this.endpoint = endpoint;
        return this;
    }

    public ApiClientBuilder lwaAccessTokenCache(LWAAccessTokenCache lwaAccessTokenCache) {
        this.lwaAccessTokenCache = lwaAccessTokenCache;
        return this;
    }

    public ApiClientBuilder disableAccessTokenCache() {
        this.disableAccessTokenCache = true;
        return this;
    }

    public ApiClientBuilder awsAuthenticationCredentialsProvider(AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider) {
        this.awsAuthenticationCredentialsProvider = awsAuthenticationCredentialsProvider;
        return this;
    }

    // TODO Why is this not used?
    public ApiClient build() {
        if (awsAuthenticationCredentials == null) {
            throw new RuntimeException("AWSAuthenticationCredentials not set");
        }

        if (lwaAuthorizationCredentials == null) {
            throw new RuntimeException("LWAAuthorizationCredentials not set");
        }

        if (StringUtils.isEmpty(endpoint)) {
            throw new RuntimeException("Endpoint not set");
        }

        AWSSigV4Signer awsSigV4Signer;
        if ( awsAuthenticationCredentialsProvider == null) {
            awsSigV4Signer = new AWSSigV4Signer(awsAuthenticationCredentials);
        }
        else {
            awsSigV4Signer = new AWSSigV4Signer(awsAuthenticationCredentials,awsAuthenticationCredentialsProvider);
        }

        LWAAuthorizationSigner lwaAuthorizationSigner = null;
        if (disableAccessTokenCache) {
            lwaAuthorizationSigner = new LWAAuthorizationSigner(lwaAuthorizationCredentials);
        }
        else {
            if (lwaAccessTokenCache == null) {
                lwaAccessTokenCache = new LWAAccessTokenCacheImpl();
            }
            lwaAuthorizationSigner = new LWAAuthorizationSigner(lwaAuthorizationCredentials,lwaAccessTokenCache);
        }

        return new ApiClient()
                .setAWSSigV4Signer(awsSigV4Signer)
                .setLWAAuthorizationSigner(lwaAuthorizationSigner)
                .setBasePath(endpoint);
    }

}
